/**
 * @file Store 配置
 * @author liaoxuezhi@baidu.com
 */
/* eslint fecs-indent: [0, "space", 2, 2] */
/* globals process */

import {
  createStore,
  applyMiddleware,
  compose
} from 'redux';
import thunk from 'redux-thunk';
import rootReducer from '../reducers';
import * as promise from 'redux-promise';
import {browserHistory} from 'react-router';
import {routerMiddleware} from 'react-router-redux';
import persistenceHandler from './store';
import ajaxProgress from './nprogress';
import serverPatches from './server-patches';


let combinedCreateStore = compose(serverPatches, persistenceHandler, ajaxProgress)(createStore);
const reduxRouterMiddleware = routerMiddleware(browserHistory);
const middleware = applyMiddleware(reduxRouterMiddleware, promise, thunk);

if (process.env.NODE_ENV !== 'production') {
  // https://chrome.google.com/webstore/detail/redux-devtools/lmhkpmbekcpmknklioeibfkpmmfibljd
  const devToolsExtension = window.devToolsExtension;
  if (typeof devToolsExtension === 'function') {
    combinedCreateStore = compose(serverPatches, persistenceHandler, ajaxProgress, devToolsExtension())(createStore);
  }
}

const finalCreator = middleware(combinedCreateStore);

export default initialState => finalCreator(rootReducer, initialState);
